package myWebApp;

/*第三章 垃圾收集器
 *测试垃圾回收时的二次标记
 *finalize()对垃圾回收的影响
 *jvm参数:-XX:+PrintGC -XX:+PrintGCDetails
 *2017年8月4日19:45:44
 */
public class GC2 {
	
	public static Object instance = null;
	
	private byte[] size = new byte[2*1024*1024];
	
	public static void get() {
		instance = new GC2();                                                                                                      
		
		instance = null;
		System.gc();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(instance == null);
		
		instance = null;
		System.gc();
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println(instance == null);
	}
	
	@Override
	protected void finalize() throws Throwable {
		instance = this;
	}
	
	public static void main(String[] args) {
		get();
	}
	
}

/*日志信息
[GC (System.gc()) [PSYoungGen: 4045K->2744K(38400K)] 4045K->2752K(125952K), 0.0014733 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [PSYoungGen: 2744K->0K(38400K)] [ParOldGen: 8K->2643K(87552K)] 2752K->2643K(125952K), [Metaspace: 2541K->2541K(1056768K)], 0.0034461 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
false
[GC (System.gc()) [PSYoungGen: 665K->32K(38400K)] 3309K->2675K(125952K), 0.0002336 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [PSYoungGen: 32K->0K(38400K)] [ParOldGen: 2643K->595K(87552K)] 2675K->595K(125952K), [Metaspace: 2542K->2542K(1056768K)], 0.0040840 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
true
Heap
 PSYoungGen      total 38400K, used 666K [0x00000000d5c00000, 0x00000000d8680000, 0x0000000100000000)
  eden space 33280K, 2% used [0x00000000d5c00000,0x00000000d5ca6838,0x00000000d7c80000)
  from space 5120K, 0% used [0x00000000d8180000,0x00000000d8180000,0x00000000d8680000)
  to   space 5120K, 0% used [0x00000000d7c80000,0x00000000d7c80000,0x00000000d8180000)
 ParOldGen       total 87552K, used 595K [0x0000000081400000, 0x0000000086980000, 0x00000000d5c00000)
  object space 87552K, 0% used [0x0000000081400000,0x0000000081494c90,0x0000000086980000)
 Metaspace       used 2548K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 272K, capacity 386K, committed 512K, reserved 1048576K

第一次回收,值为false;第二次回收值为true; 第一次finalize()方法得到了执行,第二次回收不再执行
 */

